import { radioChecked, searchCover, deepCopy, isString, isNumber, isArray } from "@/common.js"
// import { post } from "@/utils/network.js"
// 调整字段值
export const changeField = function (e, field) {
  e = e || ''
  const value = isString(e) || isNumber(e) || isArray(e) ? e : e.target.value
  // 更新右侧的值
  const item = {...this.item, [field]: value}
  this.$emit('setItem', item)
  // 更新整体表单的值
  let formData = searchCover(this.formData, {id: this.item.id}, v => ({...v, [field]: value}))
  formData = formData.map(v => ({...v, children: v.type == 'split' ? searchCover(v.children, {id: this.item.id}, v => ({...v, [field]: value})) : v.children}))
  this.$emit('setFormData', formData)
}
// 单选
export const selectItem = function (item) {
  const options = radioChecked(deepCopy(this.item.options), {id: item.id})
  const newItem = {...this.item, options, errmsg: ''}
  this.$emit('setItem', newItem)
  let formData = searchCover(this.formData, {id: this.item.id}, v => ({...v, options, value1: item.id, errmsg:''}))
  formData = formData.map(v => ({...v, children: v.type == 'split' ? searchCover(v.children, {id: this.item.id}, v => ({...v, options, value1: item.id, errmsg:''})) : v.children}))
  // this.$emit('setFormData', formData)
  /*关联字段设置：type：1禁用2启用*/
  const {link = '' , type = '' } = item || {}
  formData = searchCover(formData, v => link.split(',').includes(v.key), v => ({...v, disabled: type == 1, errmsg:''}))
  formData = formData.map(v => ({...v, children: v.type == 'split' ? searchCover(v.children, v => link.split(',').includes(v.key), v => ({...v, disabled: type == 1, errmsg:''})) : v.children}))
  this.$emit('setFormData', formData)
  // console.log(formData)
  eval(this.item.onChange)(item)
}
// 获取单选的选项列表
export const getOptions = async function () {
  // if(!this.item.optionsUrl) { return false }
  // const options = await post(this.item.optionsUrl, {})
  // let formData = searchCover(this.formData, {id: this.item.id}, v => ({...v, options: options}))
  // formData = formData.map(v => ({...v, children: v.type == 'split' ? searchCover(v.children, {id: this.item.id}, v => ({...v, options: options})) : v.children}))
  // this.$emit('setFormData', formData)
}
// 调整value2
export const changeValue2 = function (e) {
  const value2 = isString(e) || isNumber(e) || isArray(e) ? e : e.target.value
  this.changeField(value2, 'value2')
}